package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;

import com.jyb.qiyefuwu.view.activity.usercenter.SalesOrderDetailsActivity;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySubmitMakeoverorderResultBinding;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.link.router.PathEnterpriseServices;
import com.sgb.member.event.MemberEvent;
import com.swgk.core.common.utils.ActivityJump;

import org.greenrobot.eventbus.EventBus;

public class SumitMakeOverOrderResultActivity extends AppActivity {

    private ActivitySubmitMakeoverorderResultBinding mBinding;
    private String message;
    private boolean type;
    private String orderType;
    private CountDownTimer timer;
    private boolean isTimerStarting = true;

    @Override
    protected void initView() {
        super.initView();
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_submit_makeoverorder_result);
    }

    @Override
    protected void init() {
        super.init();

        type = getIntent().getBooleanExtra("type", true);
        message = getIntent().getStringExtra("message");
        orderType = getIntent().getStringExtra("ordertype");

        mBinding.tvTime.setVisibility(type ? View.VISIBLE : View.GONE);
        mBinding.tvImage.setImageResource(type ? R.mipmap.authen_success : R.mipmap.authen_fail);
        mBinding.tvText1.setText(type ? "订单提交成功" : "订单提交失败");
        mBinding.tvMessage.setText(type ? "秒后将前往订单详情" : message);
        mBinding.tvReturnhome.setVisibility(type ? View.VISIBLE : View.GONE);
        mBinding.tvButton.setText(type ? "查看订单" : "返回首页");
        mBinding.actionBar.showReturnBack(false);
        mBinding.tvButton.setOnClickListener(view -> {
            if (type) {
                startOrderDetail();
            } else {
                EventBus.getDefault().post(new MemberEvent().setMessageTag(EventConstant.EVENT_REPORT_INQUIRE_FINISH));
                MainActivity.start(getActivity());
                isTimerStarting = false;
                finish();
            }
        });

        mBinding.tvReturnhome.setOnClickListener(view -> {
            EventBus.getDefault().post(new MemberEvent().setMessageTag(EventConstant.EVENT_REPORT_INQUIRE_FINISH));
            MainActivity.start(getActivity());
            isTimerStarting = false;
            finish();
        });

        if (type) {
            mCountDownTimer_start();
        }
    }


    private void mCountDownTimer_start() {
        timer = new CountDownTimer(5000, 1000) {

            @Override
            public void onTick(long l) {
                mBinding.tvTime.setText("0" + (l / 1000));
            }

            @Override
            public void onFinish() {
                if(isTimerStarting) {
                    startOrderDetail();
                }
            }
        };
        timer.start();
    }


    private void startOrderDetail() {
        if (!TextUtils.isEmpty(orderType) && !TextUtils.isEmpty(message)) {
            EventBus.getDefault().post(new MemberEvent().setMessageTag(EventConstant.EVENT_REPORT_INQUIRE_FINISH));
            ActivityJump.startActivity(PathEnterpriseServices.PurchaseOrderDetailsActivity,
                    Pair.create("oderType", 1),
                    Pair.create("zhiChanType",
                            "商标".equals(orderType) ? SalesOrderDetailsActivity.TRADE_MARK :
                                    "专利".equals(orderType) ? SalesOrderDetailsActivity.PATENT :
                                            SalesOrderDetailsActivity.COPYRIGHT),
                    Pair.create("oderId", message));
            isTimerStarting = false;
            finish();
        }
    }

    public static void startactivity(Context mContext, boolean type, String message, String ordertye) {
        Log.e("message", ordertye);
        Intent mIntent = new Intent(mContext, SumitMakeOverOrderResultActivity.class);
        mIntent.putExtra("type", type);
        mIntent.putExtra("message", message);
        mIntent.putExtra("ordertype", ordertye);
        mContext.startActivity(mIntent);
    }

    /**
     * 释放定时器
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(timer!=null) {
            timer.cancel();
            timer.onFinish();
            timer = null;
        }
        isTimerStarting = false;
    }
}
